package abrindoojogo.exemplos.atualizacaoativa;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class MeuJogo extends JFrame implements KeyListener
{
    int tom;
    BufferStrategy bs;

    public MeuJogo()
    {
	setTitle("Atualização Ativa");
	setSize(800, 600);
	setUndecorated(true);
	setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	setIgnoreRepaint(true);
	addKeyListener(this);
	tom = 0;
    }

    public void initialize()
    {
	createBufferStrategy(1);
	bs = getBufferStrategy();
	getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
	getGraphicsConfiguration().getDevice().setDisplayMode(new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN));
	render();
    }

    public void render()
    {
	Graphics2D g = (Graphics2D) bs.getDrawGraphics();
	g.clearRect(0, 0, getWidth(), getHeight());
	for (int x = 0; x < 800; x += 10)
	{
	    for (int y = 0; y < 600; y += 10)
	    {
		g.setColor(new Color(x % 256, y % 256, (tom * 50) % 256));
		g.fillRect(x, y, 10, 10);
	    }
	}
	g.dispose();
	bs.show();
    }

    public void keyPressed(KeyEvent e)
    {
	if (e.getKeyCode() == KeyEvent.VK_ENTER)
	{
	    tom++;
	    render();
	}
    }

    public void keyReleased(KeyEvent e)
    {
    }

    public void keyTyped(KeyEvent e)
    {
    }
}
